home *** CD-ROM | disk | FTP | other *** search
- /* LIFELITE.CPP BY IAN SHARPE
-
- ****************************
- LIFE LITE PRIMER EDITION
- ****************************
-
- Comments in file LIFELITE.TXT
-
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <conio.h>
- #include <dos.h>
-
- ////////////////////////////
- #define HSIZE 40
- #define VSIZE 24
-
- #define KEEP_GOING 1
- #define QUIT 0
-
- #define DEAD 0
- #define LIVE 1
-
- #define CURSOR_UP 72
- #define CURSOR_DOWN 80
- #define CURSOR_LEFT 75
- #define CURSOR_RIGHT 77
-
- ////////////////////////////
- int world[VSIZE+2][HSIZE+2][2];
-
- ////////////////////////////
- void tidyup(void)
- {
- textcolor(WHITE);
- textbackground(BLACK);
- clrscr();
- _setcursortype(_NORMALCURSOR);
- }
-
- ////////////////////////////
- void showcell(int worldrow, int worldcol, int state)
- {
- gotoxy( (worldcol*2)-1, worldrow);
- textcolor(RED);
- textbackground(WHITE);
- cputs( (state == LIVE) ? "██" : " " );
- }
-
- ////////////////////////////
- void movecursor(int fromx, int fromy, int tox, int toy)
- {
- showcell(fromy, fromx, world[fromy][fromx][0]);
- gotoxy( (tox*2)-1, toy);
- textcolor( (world[toy][tox][0]==LIVE) ? WHITE : BLACK );
- textbackground( (world[toy][tox][0]==LIVE) ? RED : WHITE );
- cputs("≡≡");
- }
-
- ////////////////////////////
- int input_seed(void)
- {
- int x, y, nx, ny;
-
- for(y=0; y<=VSIZE+1; y++)
- for(x=0; x<=HSIZE+1; x++)
- world[y][x][0]=world[y][x][1]=DEAD;
-
- _setcursortype(_NOCURSOR);
- textbackground(WHITE);
- clrscr();
-
- textcolor(YELLOW);
- textbackground(GREEN);
- gotoxy(1,25);
- clreol();
- printf(" [Arrow keys]=move round * [Spacebar]=toggle cell * [Return]=breed * [Q]=quit");
-
- textcolor(RED);
- textbackground(WHITE);
- x=HSIZE/2;
- y=VSIZE/2;
- movecursor(x, y, x, y);
-
- while( 1 )
- switch( toupper(getch()) )
- {
- case '\r':
- return(KEEP_GOING);
- case 'Q':
- return(QUIT);
- case ' ':
- world[y][x][0] ^= LIVE;
- showcell(y, x, world[y][x][0]);
- movecursor(x, y, x, y);
- break;
- case '\0':
- nx=x; ny=y;
- switch( getch() )
- {
- case CURSOR_UP:
- ny--;
- break;
- case CURSOR_DOWN:
- ny++;
- break;
- case CURSOR_LEFT:
- nx--;
- break;
- case CURSOR_RIGHT:
- nx++;
- }
- nx=( nx==0 || nx==HSIZE+1 ) ? x : nx;
- ny=( ny==0 || ny==VSIZE+1 ) ? y : ny;
- movecursor(x, y, nx, ny);
- x=nx; y=ny;
- }
- }
-
- ////////////////////////////
- int breed(void)
- {
- int x, y, oldgen, newgen, sum, healthy;
-
- gotoxy(1, 25);
- textcolor(YELLOW);
- textbackground(BLUE);
- clreol();
- gotoxy(23, 25);
- printf("[Return]=edit new seed * [Q]=quit");
-
- textcolor(RED);
- textbackground(WHITE);
-
- oldgen=0; newgen=1; healthy=1;
- while( 1 )
- {
- while( kbhit() == 0 )
- {
- if( ! healthy )
- {
- gotoxy(30,12);
- cputs("They're dead, Jim");
- continue;
- }
-
- healthy=0;
- for(y=1; y<=VSIZE; y++)
- for(x=1; x<=HSIZE; x++)
- {
- sum=world[y-1][x-1][oldgen]
- +world[y-1][x][oldgen]
- +world[y-1][x+1][oldgen]
- +world[y][x-1][oldgen]
- +world[y][x+1][oldgen]
- +world[y+1][x-1][oldgen]
- +world[y+1][x][oldgen]
- +world[y+1][x+1][oldgen];
-
- switch( sum )
- {
- case 2:
- world[y][x][newgen]= ( world[y][x][oldgen] == LIVE ) ? LIVE : DEAD;
- break;
- case 3:
- world[y][x][newgen]=LIVE;
- break;
- default:
- world[y][x][newgen]=DEAD;
- }
- showcell(y, x, world[y][x][newgen]);
- healthy+=sum;
- }
- oldgen ^= 1; newgen ^= 1;
- }
-
- switch( toupper(getch()) )
- {
- case '\r':
- return(KEEP_GOING);
- case 'Q':
- return(QUIT);
- }
- }
- }
-
- ////////////////////////////
- void main(void)
- {
- int action;
-
- action=KEEP_GOING;
- while ( action == KEEP_GOING )
- {
- if( (action=input_seed()) == QUIT )
- continue;
- action=breed();
- }
-
- tidyup();
- }
-
- ////////////////////////
- // THAT'S ALL, FOLKS! //
- ////////////////////////
-